package com.ujoin.dao.imp;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.SysInfo;
import com.ujoin.dao.ISysInfoDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysInfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.ujoin.bean.SysInfo
 * @author MyEclipse Persistence Tools
 */

public class SysInfoDAO extends HibernateDaoSupport implements ISysInfoDAO {
	private static final Logger log = LoggerFactory.getLogger(SysInfoDAO.class);
	// property constants
	public static final String SENDER_ID = "senderId";
	public static final String RECEIVER_ID = "receiverId";
	public static final String MESSAGE = "message";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#save(com.ujoin.bean.SysInfo)
	 */
	@Override
	public void save(SysInfo transientInstance) {
		log.debug("saving SysInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#delete(com.ujoin.bean.SysInfo)
	 */
	@Override
	public void delete(SysInfo persistentInstance) {
		log.debug("deleting SysInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#findById(java.lang.Integer)
	 */
	@Override
	public SysInfo findById(java.lang.Integer id) {
		log.debug("getting SysInfo instance with id: " + id);
		try {
			SysInfo instance = (SysInfo) getHibernateTemplate().get(
					"com.ujoin.bean.SysInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#findByExample(com.ujoin.bean.SysInfo)
	 */
	@Override
	public List findByExample(SysInfo instance) {
		log.debug("finding SysInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SysInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from SysInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#findBySenderId(java.lang.Object)
	 */
	@Override
	public List findBySenderId(Object senderId) {
		return findByProperty(SENDER_ID, senderId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#findByReceiverId(java.lang.Object)
	 */
	@Override
	public List findByReceiverId(Object receiverId) {
		return findByProperty(RECEIVER_ID, receiverId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#findByMessage(java.lang.Object)
	 */
	@Override
	public List findByMessage(Object message) {
		return findByProperty(MESSAGE, message);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all SysInfo instances");
		try {
			String queryString = "from SysInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#merge(com.ujoin.bean.SysInfo)
	 */
	@Override
	public SysInfo merge(SysInfo detachedInstance) {
		log.debug("merging SysInfo instance");
		try {
			SysInfo result = (SysInfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#attachDirty(com.ujoin.bean.SysInfo)
	 */
	@Override
	public void attachDirty(SysInfo instance) {
		log.debug("attaching dirty SysInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ISysInfoDAO#attachClean(com.ujoin.bean.SysInfo)
	 */
	@Override
	public void attachClean(SysInfo instance) {
		log.debug("attaching clean SysInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ISysInfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ISysInfoDAO) ctx.getBean("SysInfoDAO");
	}
}